<template>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          销售意向管理
        </h1>
        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li class="active">销售意向管理</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
              <div class="box box-solid">
                <div class="box-header">
                  <h3 class="box-title">销售意向</h3>

                  <div class="box-tools">
                    <div class="input-group input-group-sm" style="width: 350px;">
                      <input type="text" name="table_search" class="form-control pull-right" placeholder="客户名称/合作意向/联系人/电话" style="margin-right:10px;" v-model="params.keyword">
                      <div class="input-group-btn">
                        <button type="button" class="btn btn-default" title="搜索" @click="search"><i class="fa fa-search"></i></button>
                        <button type="button" class="btn btn-default" title="导出" @click="download"><i class="fa fa-download"></i></button>
                        <router-link class="btn btn-default" :to="{name:'SaleIntentionEdit',params:{id:0}}"><i class="fa fa-plus"></i></router-link>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                  <table class="table table-bordered table-hover">
                      <thead>
                    <tr>
                      <th>客户名称</th>
                      <th>合作意向</th>
                      <th>联系人</th>
                      <th>联系电话</th>
                      <th>创建人</th>
                      <th>创建时间</th>
                      <th style="width:200px;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="model in pageModel.data" :id="'row_'+model.id">
                      <td>{{model.custom_name}}</td>
                      <td>{{model.intention}}</td>
                      <td>{{model.contact_name}}</td>
                      <td>{{model.contact_mobile}}</td>
                      <td>{{model.user.name}}</td>
                      <td>{{model.created_at}}</td>
                      <td><router-link :to="{ name: 'SaleIntentionAssign', params: {id:model.id} }">指派</router-link> | <router-link :to="{ name: 'SaleIntentionEdit',params:{id:model.id}}">编辑</router-link> | <a @click="deleteModel(model.id)">删除</a></td>
                    </tr>

                    </tbody>
                  </table>
                </div>
                <!-- /.box-body -->
                <div class="box-footer clearfix">
                  <ul class="pagination pagination-md pull-right no-margin">
                    <li><a href="#">&laquo;</a></li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">&raquo;</a></li>
                  </ul>
                </div>
              </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>

// import Pager from '@/components/Pager'
import SaleIntention from '@/modules/SaleIntention'

export default {
    mounted() {
        //更新查询参数到params上
        this.refreshParams();
        this.loadList();
    },
    data() {
        return {
            params:{
                keyword:'',
                page:1
            },
            pageModel:{}
        }
    },
    methods:{
        //更新查询参数到params上
        refreshParams(){
            this.params.keyword = this.$route.query.keyword != undefined ? this.$route.query.keyword : '';
            this.params.page = this.$route.query.page != undefined ? this.$route.query.page : 1;
        },

        loadList(){
            var self = this;
            var params = {
                status:0,//只查询没有分配的销售机会
                ...this.params
            };
            SaleIntention.list(params)
            .then((response) => {
                self.pageModel = response.contents;
            });
        },

        search() {
            var query = {
                ...this.params
            };
            query.page = 1;

            //重新查询
            this.$router.push({path:this.$route.path,query:query});
        },

        download(){
            SaleIntention.download(this.$route.query);
        },

        deleteModel(id){
            if(!confirm('确认删除吗?')){
                return;
            }
            SaleIntention.delete(id)
            .then((response) => {
                $('#row_'+id).fadeOut();
            });
        }
    },
    beforeRouteUpdate(to,from,next) {
        console.log('SaleIntention beforeRouteUpdate');
        next();
        this.$nextTick(function(){
            this.refreshParams();
            this.loadList();
        });
    },
    components:{
        // pager:Pager
    }
}
</script>
